'use strict';

angular.module('app').controller('accessStatisController', [ '$rootScope', '$scope', '$http', '$state','toaster','$modal',
    function($rootScope, $scope, $http, $state,toaster,$modal) {
        $scope.title = '门禁监控统计';
        $scope.loading = false;
        var Nowdate=new Date();
        var years = Number(Nowdate.getFullYear());
        var Month=Number(Nowdate.getMonth())+1;
        if(Month<10){
            Month = '0'+Month
        }
        var nowMonth = years+''+Month;//获取当前年月
        $('#time').attr('placeholder','请输入年月如  '+nowMonth)
        $scope.nowMonth = nowMonth;
        getstatis({'month':nowMonth});//向后台申请数据(默认加载当前月数据)
        //查询请求数据
        $scope.searchBtn = function () {
            var obj = $('#time').val();
            if(obj==''){
                getstatis({'month':nowMonth});//请求数据
            }else{
                getstatis({'month':obj});//请求数据  
            }
            
        }

        function getstatis(obj) {
            $scope.loading = true;
            var myChart = echarts.init(document.getElementById('authoStatis'));
            // 异步加载数据
            $.ajax({
                type: 'POST',
                dataType: 'json',
                contentType:'application/json;charset=UTF-8',
                url : '/dac/report/person/device',
                data: angular.toJson(obj)
            }).then(function (result) {
                $scope.loading = false;
                console.log(result)
                var option = {
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross',
                            crossStyle: {
                                color: '#999'
                            }
                        }
                    },
                    toolbox: {
                        feature: {
                            dataView: {show: true, readOnly: false},
                            magicType: {show: true, type: ['line', 'bar']},
                            restore: {show: true},
                            saveAsImage: {show: true}
                        }
                    },
                    legend: {
                        data: ['门禁接入量', '门禁故障次数','监控警报次数']
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: result.data.regionName,
                            axisPointer: {
                                type: 'shadow'
                            },
                            axisLabel:{
                                interval:0,
                                rotate:45//倾斜度 -90 至 90 默认为0
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            name: '次数',
                            min: 0,
                            interval:'',
                            axisLabel: {
                                formatter: '{value} 次'
                            }
                        },

                    ],
                    series: [
                        {
                            name: '门禁接入量',
                            type: 'bar',
                            barWidth:'20px',
                            data: result.data.accessCount,
                            itemStyle: {
                                normal: {color: '#6490f2'}
                            },
                            label: {
                                normal: {
                                    show: true,
                                    position: 'top',
                                    color:'#555'
                                }
                            },
                        },
                        {
                            name: '门禁故障次数',
                            type: 'bar',
                            barWidth:'20px',
                            data: result.data.faultCount,
                            itemStyle: {
                                normal: {color: '#ec0e6e'}
                            },
                            label: {
                                normal: {
                                    show: true,
                                    position: 'top',
                                    color:'#555'
                                }
                            },
                        },

                        {
                            name: '监控警报次数',
                            type: 'line',
                            data: result.data.warnCount,
                            itemStyle: {
                                normal: {color: 'lightgreen'}
                            },
                            label: {
                                normal: {
                                    show: true,
                                    position: 'top',
                                    color:'#555'
                                }
                            },
                        }

                    ]

                };
                myChart.setOption(option);
            })
        }
    }

]);

